﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmPhongBan : Form
    {
        public frmPhongBan()
        {
            InitializeComponent();
        }
        clsBULTT_PhongBan cls;
        clsBULHT_Ca clsPhongBan = new clsBULHT_Ca();

        private void KhoaControl(bool b)
        {
            txtMaPB.Enabled = b;
            cboPBCT.Enabled = b;
            txtTenPB.Enabled = b;
            cboCa.Enabled = b;
        }
        public void LoadDuLieu()
        {
            cls = new clsBULTT_PhongBan();
            
            dgrPhongBan.DataSource = cls.SelectAll();
            KhoaControl(false);
            cboPBCT.DataSource = cls.SelectAll();
            cboPBCT.DisplayMember = "Name";
            cboPBCT.ValueMember = "PhongBan_ID";

            cboCa.DataSource = clsPhongBan.SelectAll();
            cboCa.DisplayMember = "TenCa";
            cboCa.ValueMember = "CaID";

            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaPB.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa.");
            }
            else
            {
                DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không?", "Xóa dữ liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (hoi == DialogResult.Yes)
                {
                    try
                    {
                        cls = new clsBULTT_PhongBan();
                        cls.PhongBan_ID = txtMaPB.Text;
                        cls.Delete();
                        txtMaPB.Text = "";
                        txtTenPB.Text = "";
                        cboPBCT.SelectedIndex = -1; ;
                        LoadDuLieu();
                        MessageBox.Show("Xóa thành công !");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Không xóa được.");
                    }
                }
            }
        }

        private void frmPhongBan_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text = "&Lưu Thêm";
                txtMaPB.Text = "";
                cboPBCT.Text = "";
                txtTenPB.Text = "";
                KhoaControl(true);
                txtMaPB.Focus();
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
            }
            else
            {
                if (txtMaPB.Text == "" ||txtTenPB.Text == "")
                {
                    MessageBox.Show("Thông tin chưa đầy đủ","Thiếu thông tin",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
                else
                {                    
                    cls = new clsBULTT_PhongBan();                    
                    cls.PhongBan_ID = txtMaPB.Text;
                    cls.Name = txtTenPB.Text;
                    cls.CaID = cboCa.SelectedValue.ToString();
                    if (cboPBCT.Text == "")
                    {
                        cls.PhongBanCapTren = txtMaPB.Text;
                    }
                    else
                    {
                        cls.PhongBanCapTren = cboPBCT.Text;
                    }
                        if (cls.TrungMa() == true)
                        {
                            MessageBox.Show("Trùng mã", "Lỗi Trùng", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            cls.Insert();
                            LoadDuLieu();
                            MessageBox.Show("Thêm thành công!");
                            btnThem.Enabled = true;
                            btnXoa.Enabled = true;
                        }
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (txtMaPB.Text == ""||cboCa.Text=="")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để sửa.", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    
                    KhoaControl(true);
                    txtMaPB.Enabled = false;
                    txtTenPB.Focus();
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrPhongBan.Enabled = false;
                }
            }
            else
            {
                try
                {                    
                    cls = new clsBULTT_PhongBan();
                    cls.PhongBan_ID = txtMaPB.Text;
                    cls.Name = txtTenPB.Text;
                    cls.PhongBanCapTren = cboPBCT.SelectedValue.ToString();
                    cls.CaID = cboCa.SelectedValue.ToString();
                    if (txtMaPB.Text == "" || cboPBCT.Text == "" || txtTenPB.Text == "")
                    {
                        MessageBox.Show("Không để trống !", "Lỗi dữ liêu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        cls.Update();
                        LoadDuLieu();
                        MessageBox.Show("Sửa thành công !");
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                        dgrPhongBan.Enabled = true;
                    }                    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Không sửa được !");
                }
            }
        }

        private void dgrPhongBan_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaPB.Text = dgrPhongBan.CurrentRow.Cells[0].Value.ToString();
            txtTenPB.Text = dgrPhongBan.CurrentRow.Cells[1].Value.ToString();
            cboPBCT.Text = dgrPhongBan.CurrentRow.Cells[2].Value.ToString();
            cboCa.Text = dgrPhongBan.CurrentRow.Cells[3].Value.ToString();
        }

        private void dgrPhongBan_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnSua.Enabled = true;
            btnThem.Enabled = true;
            btnXoa.Enabled = true;
            KhoaControl(false);
            btnThem.Text = "&Thêm";
            btnSua.Text = "&Sửa";
        }
    }
}
